View Javadoc

1   package net.sf.bse.gui;
2   
3   /*
4    * Copyright (c) 2002-2003 BSE project contributors 
5    * (http://bse.sourceforge.net/)
6    * 
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  import java.awt.event.MouseAdapter;
29  import java.awt.event.MouseEvent;
30  import java.awt.event.MouseListener;
31  import java.io.File;
32  
33  import javax.swing.JMenuItem;
34  import javax.swing.JPopupMenu;
35  import javax.swing.JTree;
36  import javax.swing.event.TreeModelEvent;
37  import javax.swing.event.TreeModelListener;
38  
39  /***
40   * @author Aleksi Peebles (aleksi.peebles@infocast.fi)
41   * @version $Revision: 1.1 $ $Date: 2003/01/17 14:40:27 $
42   */
43  public class XletTree extends JTree implements ActionListener, TreeModelListener
44  {    
45      private JPopupMenu popup;
46      
47      public XletTree()
48      {
49          this(new File(System.getProperty("user.dir")));
50      }
51      
52      public XletTree(File root)
53      {
54          super(new XletTreeModel(root));
55          setCellRenderer(new XletTreeCellRenderer());
56          getModel().addTreeModelListener(this);
57          
58          popup = new JPopupMenu();
59          JMenuItem item = new JMenuItem("Set Signed");
60          item.addActionListener(this);
61          popup.add(item);
62          item = new JMenuItem("Set Unsigned");
63          item.addActionListener(this);
64          popup.add(item);
65          
66          MouseListener popupListener = new PopupListener();
67          addMouseListener(popupListener);
68      }
69      
70      public void setRoot(File root)
71      {
72          ((XletTreeModel)getModel()).setRoot(root);
73      }
74      
75      public File getRoot()
76      {
77          return (File)((XletTreeModel)getModel()).getRoot();
78      }
79      
80      public void setRecursiveSelection(boolean recursive)
81      {
82          ((XletTreeModel)getModel()).setRecursiveSelection(recursive);
83      }
84      
85      public boolean isRecursiveSelection()
86      {
87          return ((XletTreeModel)getModel()).isRecursiveSelection();
88      }
89      
90      public String[] getSignFileNames()
91      {
92          return ((XletTreeModel)getModel()).getSignFileNames();
93      }
94      
95      //
96      // ActionListener interface method
97      //
98      
99      /*** 
100      * Invoked when a popup menu item is selected.
101      */
102     public void actionPerformed(ActionEvent e)
103     {
104         XletTreeModel model = (XletTreeModel)getModel();
105         if (e.getActionCommand().equals("Set Signed"))
106         {
107             // Hopefully other algorithms will be supported in the future...
108             model.setSigned(getSelectionModel(), "MD5");
109         }
110         else
111         {
112             model.setUnsigned(getSelectionModel());
113         }
114     }
115     
116     //
117     // TreeModelListener interface methods
118     //
119     
120     public void treeNodesChanged(TreeModelEvent e)
121     {
122         repaint();
123     }
124     
125     public void treeNodesInserted(TreeModelEvent e)
126     {
127     }
128     
129     public void treeNodesRemoved(TreeModelEvent e)
130     {
131     }
132     
133     public void treeStructureChanged(TreeModelEvent e)
134     {
135     }
136     
137     /***
138      * Popup menu listener class
139      */    
140     private class PopupListener extends MouseAdapter
141     {
142         public void mousePressed(MouseEvent e) 
143         {
144             maybeShowPopup(e);
145         }
146 
147         public void mouseReleased(MouseEvent e)
148         {
149             maybeShowPopup(e);
150         }
151 
152         private void maybeShowPopup(MouseEvent e)
153         {
154             if (getSelectionCount() > 0 && e.isPopupTrigger())
155             {
156                 popup.show(e.getComponent(), e.getX(), e.getY());
157             }
158         }
159     }
160 }